package com.hellpro.lolbox.video.view;


import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.BaseFragment;
import com.hellpro.lolbox.factory.VideoFragmentFactory;
import com.hellpro.lolbox.utils.UIManger;
import com.hellpro.lolbox.video.presenter.VideoPresenter;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.utils.Log;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 类描述:
 * 创建人:阿犇
 * 创建时间:2016/10/18
 * 备注:Video模块的类
 */
public class VideoFragment extends BaseFragment implements IVideoView {


    @BindView(R.id.video_iv_search)
    ImageView videoIvSearch;
    @BindView(R.id.video_iv_downLoad)
    ImageView videoIvDownLoad;
    @BindView(R.id.video_tab_layout)
    TabLayout videoTabLayout;
    @BindView(R.id.video_vp)
    ViewPager videoVp;

    @Override
    public void initView() {
        videoTabLayout.setTabTextColors(Color.GRAY, Color.parseColor("#FFC13D"));//设置选择和未选择的颜色
        videoTabLayout.setupWithViewPager(videoVp, true);//设置tablayout和viewpager关联 是否自动刷新
        videoTabLayout.setTabMode(TabLayout.MODE_FIXED);//设置导航的模式
        videoTabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FFB20D"));//设置指示器的颜色
        videoTabLayout.setSelectedTabIndicatorHeight(4);//设置指示器的宽度

        VideoPresenter presenter = new VideoPresenter(this);
        //设置ViewPager的适配器
        presenter.setViewPagerAdapter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_video;
    }

    /**
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/18
     * 备注:获取tabLayout的标题信息的方法
     */
    @Override
    public String[] getTitles() {
        return getResources().getStringArray(R.array.video_tab_array);
    }

    /**
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/18
     * 备注:获取Video模块中所有Fragment的方法
     */
    @Override
    public List<Fragment> getContentFragments() {
        List<Fragment> list = new ArrayList<>();
        for (int i = 0; i < getTitles().length; i++) {
            Fragment fragment = VideoFragmentFactory.createFragment(i);
            list.add(fragment);
        }
        return list;
    }

    /**
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/18
     * 备注:获取TabLayout的方法
     */
    @Override
    public TabLayout getContentTabLayout() {
        return videoTabLayout;
    }

    /**
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/18
     * 备注:获取ViewPager的方法
     */
    @Override
    public ViewPager getContentViewPager() {
        return videoVp;
    }

    /**
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/18
     * 备注:获取FragmentManager的方法
     */
    @Override
    public FragmentManager getVideoFragmentManager() {

        return getFragmentManager();
    }

    /**
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/18
     * 备注:获取搜索图片的点击事件
     */
    @Override
    public ImageView getSearchImageView() {
        return videoIvSearch;
    }

    /**
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/18
     * 备注:获取下载图片的点击事件
     */
    @Override
    public ImageView getDownLoadImageView() {
        return videoIvDownLoad;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @OnClick({R.id.video_iv_search, R.id.video_iv_downLoad})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.video_iv_search:
                UIManger.startToActivity(getActivity(),VideoSearchActivity.class);
                break;
            case R.id.video_iv_downLoad:

                new ShareAction(getActivity()).withTargetUrl("getActivity()").withTitle("LOL盒子")
                        .setDisplayList(SHARE_MEDIA.SINA,SHARE_MEDIA.QQ,SHARE_MEDIA.WEIXIN)
                        .setCallback(umShareListener).open();
                break;
        }
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {

            Toast.makeText(getActivity(), platform + " 分享成功啦", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(getActivity(),platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
            if(t!=null){
                Log.d("throw","throw:"+t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(getActivity(),platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };
}
